`
Elements of a Bash Script
In this section, you’ll learn the building blocks of a bash script,
including how to use comments to document what a script does, how
to tell Linux to use a specific interpreter to execute the script, and
how to style your scripts for better readability.
Bash doesn’t have an official style guide, but we recommend
adhering to Google’s shell style guide
(https://google.github.io/styleguide/shellguide.html), which outlines
best practices to follow when developing bash code. If you work on
a team of penetration testers and have an exploit code repository,
using good code styling practices will help your team maintain it.
The Shebang Line
Every script should begin with the shebang line, a character
sequence that starts with the hash and exclamation mark symbols
(#!), followed by the full path to the script interpreter. Listing 1-3
shows an example of a shebang line for a typical bash script.
#!/bin/bash
--snip--
Listing 1-3
The shebang line
The bash interpreter is typically located at /bin/bash. If you
instead wrote scripts in Python or Ruby, your shebang line would
include the full path to the Python or Ruby interpreter.
You’ll sometimes encounter bash scripts that make use of a
shebang line like the following.
#!/usr/bin/env bash
--snip—
Listing 1-4
A portable shebang line
You may want to use this shebang line because it is more
portable than the one in Listing 1-3. Some Linux distributions place
the bash interpreter in different system locations, and this shebang
line will attempt to find that location. This approach could be
particularly useful in penetration tests, where you might not know
the location of the bash interpreter on the target machine. For
simplicity, however, we’ll use the shebang version from Listing 1-3
throughout this book.
Black Hat Bash (Early Access) © 2023 by Dolev Farhi and Nick Aleks